package com.zadtek.sharkRider.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.zadtek.sharkRider.R;
import com.zadtek.sharkRider.activity.common.Const;
import com.zadtek.sharkRider.frame.MyApplication;
import com.zadtek.sharkRider.receiver.ExampleUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class BaseActivity extends Activity {

	// LOG标志
	private String Tag = "BaseActivity";
	Intent intent;

	protected boolean isAddActivity = true; // 是否将activity加入集合中
	// 进度对话框
	private ProgressDialog baseProgressDialog = null;

	public static int pwidth = 480;
	public static int pheigth = 854;
	
	public Handler mHandler;
	
	/**
	 * 创建
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 设置全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏

		if (isAddActivity) {
			MyApplication.getInstace().addActivity(this);
		}

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		pwidth = dm.widthPixels;
		pheigth = dm.heightPixels;

	}

	/**
	 * 退出提示框
	 */
	public void ExitDialog() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setMessage("您确认退出吗？");
		alert.setTitle("确定退出");
		// alert.setIcon(R.drawable.menu_back_01);
		alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// exitFrame();
			}
		});

		alert.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		alert.create().show();
	}

	/**
	 * 显示等待进度框
	 * 
	 * @param str
	 */
	public void showProgressDialog(String str) {
		this.baseProgressDialog = new ProgressDialog(this);
		this.baseProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		this.baseProgressDialog.setIndeterminate(false);
		this.baseProgressDialog.setMessage(str);
		this.baseProgressDialog.setCancelable(true);
		this.baseProgressDialog.show();
	}

	/**
	 * 显示等待进度框,点返回没反应
	 * 
	 * @param str
	 */
	public void showProgressDiaNo(String str) {
		this.baseProgressDialog = new ProgressDialog(this);
		this.baseProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		this.baseProgressDialog.setIndeterminate(false);
		this.baseProgressDialog.setMessage(str);
		this.baseProgressDialog.setCancelable(false);
		this.baseProgressDialog.show();
	}

	/**
	 * 更新等待进度框内容
	 * 
	 * @param str
	 */
	public void setProgressDialogMessage(String str) {
		if (this.baseProgressDialog != null) {
			this.baseProgressDialog.setMessage(str);
		}
	}

	/**
	 * 关闭等待进度框
	 */
	public void closeProgressDialog() {
		if (this.baseProgressDialog != null) {
			this.baseProgressDialog.dismiss();
			this.baseProgressDialog = null;
		}
	}

	// 设置推送别名
	public void setAlias(String alias) {
		if (!ExampleUtil.isValidTagAndAlias(alias)) {
			Toast.makeText(BaseActivity.this, R.string.error_tag_gs_empty,
					Toast.LENGTH_SHORT).show();
			return;
		}
		// 调用JPush API设置Alias
		mHandler.sendMessage(mHandler.obtainMessage(Const.MSG_SET_ALIAS, alias));
	}

	// 设置别名与标签方法的回调类，可在 gotResult 方法上得到回调的结果。回调 responseCode = 0，则确认设置成功。
	public final TagAliasCallback mAliasCallback = new TagAliasCallback() {
		@Override
		public void gotResult(int code, String alias, Set<String> tags) {
			String logs;
			switch (code) {
			case 0:
				logs = "Set tag and alias success:" + alias;
				break;

			case 6002:
				logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
				if (ExampleUtil.isConnected(getApplicationContext())) {
					mHandler.sendMessageDelayed(
							mHandler.obtainMessage(Const.MSG_SET_ALIAS, alias),
							1000 * 60);
				} else {
					Log.v("sd", "No network");
				}
				break;

			default:
				logs = "Failed with errorCode = " + code;
			}

			// ExampleUtil.showToast(logs, getApplicationContext());
		}

	};
	
	public String time2time(long t) {
		if (t <= 0) {
			 return "00:00:00:000";
		}

		long haomiao = 0;

		long miao = t / 1000 % 60;
		long min = t / 1000 / 60 % 60;
		long h = t / 1000 / 60 / 60;

		return String.format("%02d", h) + ":" + String.format("%02d", min)
				+ ":" + String.format("%02d", miao) + "";

	}
	
	public long time2millionSeconds(String str) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		long millionSeconds = sdf.parse(str).getTime();// 毫秒
		return millionSeconds;

	}

	
	/**
	 * 暂停
	 */
	@Override
	public void onPause() {
		super.onPause();
		JPushInterface.onPause(this);
		MobclickAgent.onPause(this);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		JPushInterface.onResume(this);
		MobclickAgent.onResume(this);
	}
}
